﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package org.aswing {
    import org.aswing.geom.*;
    import org.aswing.event.*;
    import org.aswing.plaf.basic.*;

    public class JViewport extends Container implements Viewportable {

        public static const AUTO_INCREMENT:int = -2147483648;

        public static var CENTER:int = 0;
        public static var TOP:int = 1;
        public static var LEFT:int = 2;
        public static var BOTTOM:int = 3;
        public static var RIGHT:int = 4;

        private var verticalUnitIncrement:int;
        private var verticalBlockIncrement:int;
        private var horizontalUnitIncrement:int;
        private var horizontalBlockIncrement:int;
        private var tracksHeight:Boolean;
        private var tracksWidth:Boolean;
        private var verticalAlignment:int;
        private var horizontalAlignment:int;
        private var view:Component;

        public function JViewport(_arg1:Component=null, _arg2:Boolean=false, _arg3:Boolean=false){
            setName("JViewport");
            this.tracksWidth = _arg2;
            this.tracksHeight = _arg3;
            this.verticalUnitIncrement = AUTO_INCREMENT;
            this.verticalBlockIncrement = AUTO_INCREMENT;
            this.horizontalUnitIncrement = AUTO_INCREMENT;
            this.horizontalBlockIncrement = AUTO_INCREMENT;
            this.verticalAlignment = CENTER;
            this.horizontalAlignment = CENTER;
            if (_arg1 != null){
                this.setView(_arg1);
            };
            this.setLayout(new ViewportLayout());
            this.updateUI();
        }
        override public function updateUI():void{
            setUI(UIManager.getUI(this));
        }
        override public function getDefaultBasicUIClass():Class{
            return (BasicViewportUI);
        }
        override public function getUIClassID():String{
            return ("ViewportUI");
        }
        override public function setLayout(_arg1:LayoutManager):void{
            if ((_arg1 is ViewportLayout)){
                super.setLayout(_arg1);
            } else {
                throw (new Error("Only on set ViewportLayout to JViewport"));
            };
        }
        public function setTracksWidth(_arg1:Boolean):void{
            if (_arg1 != this.tracksWidth){
                this.tracksWidth = _arg1;
                revalidate();
            };
        }
        public function isTracksWidth():Boolean{
            return (this.tracksWidth);
        }
        public function setTracksHeight(_arg1:Boolean):void{
            if (this.tracksHeight != _arg1){
                this.tracksHeight = _arg1;
                revalidate();
            };
        }
        public function isTracksHeight():Boolean{
            return (this.tracksHeight);
        }
        public function getVerticalAlignment():int{
            return (this.verticalAlignment);
        }
        public function setVerticalAlignment(_arg1:int):void{
            if (_arg1 == this.verticalAlignment){
                return;
            };
            this.verticalAlignment = _arg1;
            this.setViewPosition(this.getViewPosition());
        }
        public function getHorizontalAlignment():int{
            return (this.horizontalAlignment);
        }
        public function setHorizontalAlignment(_arg1:int):void{
            if (_arg1 == this.horizontalAlignment){
                return;
            };
            this.horizontalAlignment = _arg1;
            this.setViewPosition(this.getViewPosition());
        }
        public function setView(_arg1:Component):void{
            if (this.view != _arg1){
                this.view = _arg1;
                removeAll();
                if (_arg1 != null){
                    insertImp(-1, _arg1);
                };
                this.fireStateChanged();
            };
        }
        public function getView():Component{
            return (this.view);
        }
        public function setVerticalUnitIncrement(_arg1:int):void{
            if (this.verticalUnitIncrement != _arg1){
                this.verticalUnitIncrement = _arg1;
                this.fireStateChanged();
            };
        }
        public function setVerticalBlockIncrement(_arg1:int):void{
            if (this.verticalBlockIncrement != _arg1){
                this.verticalBlockIncrement = _arg1;
                this.fireStateChanged();
            };
        }
        public function setHorizontalUnitIncrement(_arg1:int):void{
            if (this.horizontalUnitIncrement != _arg1){
                this.horizontalUnitIncrement = _arg1;
                this.fireStateChanged();
            };
        }
        public function setHorizontalBlockIncrement(_arg1:int):void{
            if (this.horizontalBlockIncrement != _arg1){
                this.horizontalBlockIncrement = _arg1;
                this.fireStateChanged();
            };
        }
        override public function append(_arg1:Component, _arg2:Object=null):void{
            this.setView(_arg1);
        }
        override public function insert(_arg1:int, _arg2:Component, _arg3:Object=null):void{
            this.setView(_arg2);
        }
        public function getVerticalUnitIncrement():int{
            if (this.verticalUnitIncrement != AUTO_INCREMENT){
                return (this.verticalUnitIncrement);
            };
            return (Math.max((this.getExtentSize().height / 40), 1));
        }
        public function getVerticalBlockIncrement():int{
            if (this.verticalBlockIncrement != AUTO_INCREMENT){
                return (this.verticalBlockIncrement);
            };
            return ((this.getExtentSize().height - 1));
        }
        public function getHorizontalUnitIncrement():int{
            if (this.horizontalUnitIncrement != AUTO_INCREMENT){
                return (this.horizontalUnitIncrement);
            };
            return (Math.max((this.getExtentSize().width / 40), 1));
        }
        public function getHorizontalBlockIncrement():int{
            if (this.horizontalBlockIncrement != AUTO_INCREMENT){
                return (this.horizontalBlockIncrement);
            };
            return ((this.getExtentSize().width - 1));
        }
        public function setViewportTestSize(_arg1:IntDimension):void{
            setSize(_arg1);
        }
        public function getExtentSize():IntDimension{
            return (getInsets().getInsideSize(getSize()));
        }
        public function getViewSize():IntDimension{
            var _local1:IntDimension;
            var _local2:IntDimension;
            if (this.view == null){
                return (new IntDimension());
            };
            if (((this.isTracksWidth()) && (this.isTracksHeight()))){
                return (this.getExtentSize());
            };
            _local1 = this.view.getPreferredSize();
            _local2 = this.getExtentSize();
            if (this.isTracksWidth()){
                _local1.width = _local2.width;
            } else {
                if (this.isTracksHeight()){
                    _local1.height = _local2.height;
                };
            };
            return (_local1);
        }
        public function getViewPosition():IntPoint{
            var _local1:IntPoint;
            var _local2:IntRectangle;
            if (this.view != null){
                _local1 = this.view.getLocation();
                _local2 = getInsets().getInsideBounds(getSize().getBounds());
                _local1.x = (_local2.x - _local1.x);
                _local1.y = (_local2.y - _local1.y);
                return (_local1);
            };
            return (new IntPoint(0, 0));
        }
        public function setViewPosition(_arg1:IntPoint, _arg2:Boolean=true):void{
            var _local3:IntRectangle;
            this.restrictionViewPos(_arg1);
            if (!_arg1.equals(this.getViewPosition())){
                _local3 = getInsets().getInsideBounds(getSize().getBounds());
                if (this.view){
                    this.view.setLocationXY((_local3.x - _arg1.x), (_local3.y - _arg1.y));
                };
                this.fireStateChanged(_arg2);
            };
        }
        public function scrollRectToVisible(_arg1:IntRectangle, _arg2:Boolean=true):void{
            this.setViewPosition(new IntPoint(_arg1.x, _arg1.y), _arg2);
        }
        public function ensureRectVisible(_arg1:IntRectangle, _arg2:Boolean=true):void{
            _arg1 = _arg1.clone();
            var _local3:IntPoint = this.getViewPosition();
            var _local4:IntDimension = this.getExtentSize();
            var _local5:IntDimension = this.getViewSize();
            var _local6:IntRectangle = new IntRectangle(_local3.x, _local3.y, _local4.width, _local4.height);
            if (_arg1.x < 0){
                _arg1.width = (_arg1.width + _arg1.x);
                _arg1.x = 0;
            };
            if (_arg1.y < 0){
                _arg1.height = (_arg1.height + _arg1.y);
                _arg1.y = 0;
            };
            if ((_arg1.x + _arg1.width) > _local5.width){
                _arg1.width = (_local5.width - _arg1.x);
            };
            if ((_arg1.y + _arg1.height) > _local5.height){
                _arg1.height = (_local5.height - _arg1.y);
            };
            var _local7:IntPoint = _local3.clone();
            if ((_arg1.x + _arg1.width) > (_local6.x + _local6.width)){
                _local7.x = ((_arg1.x + _arg1.width) - _local4.width);
            };
            if ((_arg1.y + _arg1.height) > (_local6.y + _local6.height)){
                _local7.y = ((_arg1.y + _arg1.height) - _local4.height);
            };
            if (_arg1.x < _local6.x){
                _local7.x = _arg1.x;
            };
            if (_arg1.y < _local6.y){
                _local7.y = _arg1.y;
            };
            this.setViewPosition(_local7, _arg2);
        }
        public function scrollVertical(_arg1:int):void{
            this.setViewPosition(this.getViewPosition().move(0, _arg1));
        }
        public function scrollHorizontal(_arg1:int):void{
            this.setViewPosition(this.getViewPosition().move(_arg1, 0));
        }
        public function scrollToBottomLeft():void{
            this.setViewPosition(new IntPoint(0, int.MAX_VALUE));
        }
        public function scrollToBottomRight():void{
            this.setViewPosition(new IntPoint(int.MAX_VALUE, int.MAX_VALUE));
        }
        public function scrollToTopLeft():void{
            this.setViewPosition(new IntPoint(0, 0));
        }
        public function scrollToTopRight():void{
            this.setViewPosition(new IntPoint(int.MAX_VALUE, 0));
        }
        protected function restrictionViewPos(_arg1:IntPoint):IntPoint{
            var _local2:IntDimension = this.getExtentSize();
            var _local3:IntDimension = this.getViewSize();
            if (_local2.width < _local3.width){
                _arg1.x = Math.max(0, Math.min((_local3.width - _local2.width), _arg1.x));
            } else {
                if (_local2.width > _local3.width){
                    if (this.horizontalAlignment == CENTER){
                        _arg1.x = (-((_local2.width - _local3.width)) / 2);
                    } else {
                        if (this.horizontalAlignment == RIGHT){
                            _arg1.x = -((_local2.width - _local3.width));
                        } else {
                            _arg1.x = 0;
                        };
                    };
                } else {
                    _arg1.x = 0;
                };
            };
            if (_local2.height < _local3.height){
                _arg1.y = Math.max(0, Math.min((_local3.height - _local2.height), _arg1.y));
            } else {
                if (_local2.height > _local3.height){
                    if (this.verticalAlignment == CENTER){
                        _arg1.y = (-((_local2.height - _local3.height)) / 2);
                    } else {
                        if (this.verticalAlignment == BOTTOM){
                            _arg1.y = -((_local2.height - _local3.height));
                        } else {
                            _arg1.y = 0;
                        };
                    };
                } else {
                    _arg1.y = 0;
                };
            };
            return (_arg1);
        }
        public function addStateListener(_arg1:Function, _arg2:int=0, _arg3:Boolean=false):void{
            addEventListener(InteractiveEvent.STATE_CHANGED, _arg1, false, _arg2);
        }
        public function removeStateListener(_arg1:Function):void{
            removeEventListener(InteractiveEvent.STATE_CHANGED, _arg1);
        }
        protected function fireStateChanged(_arg1:Boolean=true):void{
            dispatchEvent(new InteractiveEvent(InteractiveEvent.STATE_CHANGED, _arg1));
        }
        public function getViewportPane():Component{
            return (this);
        }

    }
}//package org.aswing 
